<?php

    class MY_Controller extends CI_Controller {

        protected $data;
        public $translate;
        public $activeLang;
        protected $views = array(
            7 => 'site/about'
        );
        protected $simpleNav = array(10);
        protected $removeNav = array(11, 12);

        function __construct() {
            parent::__construct();

            $this->load->library('factory');
            $this->load->model('icms');
            $this->translate = & $this->factory->create('translate');
            $this->activeLang = $this->lang->mci_current();
            require_once EXTERNAL_URL . 'urlify.php';


            $this->data['categories'] = $this->icms->categories(array(8, 9));
        }

        protected function _nav($cats, $cat_ID = NULL) {

            $superfish = "";

            $cats = $this->translate->clean_asoc_array($cats, $this->activeLang);

            foreach ($cats as $cat):

                if (!in_array($cat['ID'], $this->removeNav)):


                    $name = URLify::filter($cat['name']);
                    $url = lang_url('category/' . underscore($name) . '-' . $cat['ID']);

                    $a = "";

                    if (isset($cat_ID) && $cat_ID == $cat['ID'])
                        $a = 'class="active"';

                    $superfish.= '<li ' . $a . '><a href="' . $url . '">' . $cat['name'] . '</a>';

                    if (!in_array($cat['ID'], $this->simpleNav)):


                        //go recrusive for parent
                        $SORT_ORDER = find_sort_type($this->db, $cat['ID']);
                        $query = "WHERE category_ID=" . $cat['ID'];
                        $sql = "SELECT * FROM post $query AND parent_ID = ? ORDER BY sort $SORT_ORDER, ID $SORT_ORDER";

                        $columns = array(
                            'ID' => 'ID',
                            'title' => 'title',
                            'parent_ID' => 'parent_ID',
                            'category_ID' => 'category_ID'
                        );

                        $recordset = array();
                        recrusive($this->db, $this->translate, $this->activeLang, $sql, 0, $recordset, $columns);
                        $arrayTree = parseArrayTree($recordset, $columns, 0);

                        printList($arrayTree, $superfish, 0, $cat, FALSE);

                    endif;

                    $superfish .='</li>';

                endif;

            endforeach;

            return $superfish;
        }

        public function question() {

            require EXTERNAL_PATH . 'plugins/phpmailer/class.phpmailer.php';

            //Create a new PHPMailer instance
            $mail = new PHPMailer();
            $mail->CharSet = 'UTF-8';

            //Set who the message is to be sent from
            $mail->SetFrom('no-replay@zenski-biznis.com', 'Zenski biznis Crna Gora');

            //Set an alternative reply-to address
            $mail->AddReplyTo($_GET['email'], $_GET['name'] . ' ' . $_GET['surname']);

            //Set who the message is to be sent to
            $mail->AddAddress('ikukic@yahoo.com');

            //Set the subject line
            $mail->Subject = $_GET['from_page'] . ' | Question';

            //Read an HTML message body from an external file, convert referenced images to embedded, convert HTML into a basic plain-text alternative body
            $HTML = 'Ime: <b>' . $_GET['name'] . '</b><br>';
            $HTML .= 'Prezime: <b>' . $_GET['surname'] . '</b><br>';
            $HTML .= 'Email: <b>' . $_GET['email'] . '</b><br>';
            $HTML .= 'Mobilni telefon: <b>' . $_GET['mobile_phone'] . '</b><br>';
            $HTML .= 'Upit sa strane: <b>' . $_GET['from_page'] . '</b><br>';
            $HTML .= 'Pitanje: <br><br>' . $_GET['note'] . '<br><br>';

            $mail->MsgHTML($HTML);

            //Send the message, check for errors
            if (!$mail->Send()) {
                echo json_encode(array('success' => FALSE, 'error' => $mail->ErrorInfo));
            } else {
                echo json_encode(array('success' => TRUE, 'error' => 0));
            }
        }

        protected function findHomepage($ID, $limit = NULL) {

            $categoryFull = array();

            $parentStories = $this->findCategoryPosts($ID, $limit, NULL, 0);

            foreach ($parentStories as $parent):

                $childStories = $this->translate->res_array($this->icms->find_child_post($parent['ID'], 1), $this->activeLang);

                url_cms_name($childStories, 'title');

                foreach ($childStories as &$child):

                    $child['category'] = $parent['title'];

                    $categoryFull[] = $child;


                endforeach;

            endforeach;

            $this->_fields($categoryFull);

            return $categoryFull;
        }

        protected function findCategoryPosts($ID, $limit = NULL, $order = NULL, $parent = NULL, $unset = NULL) {

            $category = $this->translate->res_array($this->icms->find_by_cat($ID, $order, $limit, $parent), $this->activeLang);
            foreach ($category as &$value):

                $photo = $this->icms->find_top_photo($value['ID']);

                $value['photo_name'] = $photo['filename'];
                $value['photo_alttext'] = $photo['alttext'];

            endforeach;

            if (isset($unset)):

                $cnt = count($category);
                for ($i = 0; $i < $cnt; $i++):
                    if ($category[$i]["ID"] == $unset):
                        unset($category[$i]);

                    endif;
                endfor;

                $category = array_values($category);

            endif;

            url_cms_name($category, 'title');

//            $this->firephp->log($category);

            return $category;
        }

        protected function findPost($ID, $order = NULL) {

            $post = $this->translate->clean_asoc_array($this->icms->find_post_id($ID, $order), $this->activeLang);

            if (count($post) > 0):
                $photo = $this->icms->find_top_photo($post['ID']);

                $post['photo_name'] = $photo['filename'];
                $post['photo_alttext'] = $photo['alttext'];

            endif;

            return $post;
        }

        protected function _fields(&$data) {

            // do parse fields with helper
            $cnt = count($data);
            for ($i = 0; $i < $cnt; $i++)
                if (isset($data[$i]['fields']))
                    $data[$i] = fileds_row_array($data[$i], 'fields', 'field_');
        }
        
         protected function loadSeo(&$data, $seo) {

            $data['seo_title'] = $seo['seo_title'] != "" ? $seo['seo_title'] : $seo['title'];
            $data['seo_description'] = $seo['seo_description'] != "" ? $seo['seo_description'] : lang('home-description');
            $data['seo_keywords'] = $seo['seo_keywords'] != "" ? $seo['seo_keywords'] : lang('home-keywords');
        }

    }
    